import GCJ_CLASS_TYPE = require('./../../common/GCJ_CLASS_TYPE');
import Point = require('./../../common/graph/Point');
import Serializer = require('./Serializer');
import ObjPosition = require('./ObjPosition');
import ObjPositionAlign=require('./ObjPositionAlign');

export  = ObjRelativePosType;

/**
 *
 * @author 
 *
 */
class ObjRelativePosType {
    public self: number=ObjPositionAlign.ALIGN_POINT_0;
    public other: number=ObjPositionAlign.ALIGN_POINT_0;
    
    // 对齐点
    /*
    7-----0-----1
    |           |
    6     8     2
    |           |
    5-----4-----3
    * */
    
	public constructor(s?, o?) {
        if(s) {
            this.self=s;
        }
        if(o){
            this.other=o;
        }
	}
    // 从json对象(注意不是json字符串)中解析
    public fromJson(jsonObj,versionId) {
        this.self=jsonObj.self;
        this.other=jsonObj.other;
    }
    // 转成json字符串
    public toJson(): string {
        var ret='{';
                                                      
        // self
        ret+='"self":';
        ret+=this.self;
        ret+=',';
        // other
        ret+='"other":';
        ret+=this.other;

        ret+='}';
        return ret;
    }  
    
	/*上下结构*/
    public is_5_7() {
        return this.self==ObjPositionAlign.ALIGN_POINT_5
            &&this.other==ObjPositionAlign.ALIGN_POINT_7;
    }
    public is_4_0() {
        return this.self==ObjPositionAlign.ALIGN_POINT_4
            &&this.other==ObjPositionAlign.ALIGN_POINT_0;
    }
    public is_3_1() {
        return this.self==ObjPositionAlign.ALIGN_POINT_5
            &&this.other==ObjPositionAlign.ALIGN_POINT_7;
    }
    /*下上结构*/
    public is_7_5() {
        return this.self==ObjPositionAlign.ALIGN_POINT_7
            &&this.other==ObjPositionAlign.ALIGN_POINT_5;
    }
    public is_0_4() {
        return this.self==ObjPositionAlign.ALIGN_POINT_0
            &&this.other==ObjPositionAlign.ALIGN_POINT_4;
    }
    public is_1_3() {
        return this.self==ObjPositionAlign.ALIGN_POINT_1
            &&this.other==ObjPositionAlign.ALIGN_POINT_3;
    }
    /*左右结构*/
    public is_1_7() {
        return this.self==ObjPositionAlign.ALIGN_POINT_1
            &&this.other==ObjPositionAlign.ALIGN_POINT_7;
    }
    public is_2_6() {
        return this.self==ObjPositionAlign.ALIGN_POINT_2
            &&this.other==ObjPositionAlign.ALIGN_POINT_6;
    }
    public is_3_5() {
        return this.self==ObjPositionAlign.ALIGN_POINT_3
            &&this.other==ObjPositionAlign.ALIGN_POINT_5;
    }
    /*右左结构*/
    public is_7_1() {
        return this.self==ObjPositionAlign.ALIGN_POINT_7
            &&this.other==ObjPositionAlign.ALIGN_POINT_1;
    }
    public is_6_2() {
        return this.self==ObjPositionAlign.ALIGN_POINT_6
            &&this.other==ObjPositionAlign.ALIGN_POINT_2;
    }
    public is_5_3() {
        return this.self==ObjPositionAlign.ALIGN_POINT_5
            &&this.other==ObjPositionAlign.ALIGN_POINT_3;
    }
    /*对角线结构*/
    public is_3_7() {
        return this.self==ObjPositionAlign.ALIGN_POINT_3
            &&this.other==ObjPositionAlign.ALIGN_POINT_7;
    }
    public is_5_1() {
        return this.self==ObjPositionAlign.ALIGN_POINT_5
            &&this.other==ObjPositionAlign.ALIGN_POINT_1;
    }
    public is_7_3() {
        return this.self==ObjPositionAlign.ALIGN_POINT_7
            &&this.other==ObjPositionAlign.ALIGN_POINT_3;
    }
    public is_1_5() {
        return this.self==ObjPositionAlign.ALIGN_POINT_1
            &&this.other==ObjPositionAlign.ALIGN_POINT_5;
    }
    
    // TODO
    /*获取对齐方式名称*/
    public getTypeName(): string {
        var name: string='';
        if(this.is_5_7()) {
            name='上下结构:57';
        } else if(this.is_4_0()) {
            name='上下结构:40';
        } else if(this.is_3_1()) {
            name='上下结构:31';

        } else if(this.is_7_5()) {
            name='下上结构:75';
        } else if(this.is_0_4()) {
            name='下上结构:04';
        } else if(this.is_1_3()) {
            name='下上结构:13';

        } else if(this.is_1_7()) {
            name='左右结构:17';
        } else if(this.is_2_6()) {
            name='左右结构:26';
        } else if(this.is_3_5()) {
            name='左右结构:35';

        } else if(this.is_7_1()) {
            name='右左结构:71';
        } else if(this.is_6_2()) {
            name='右左结构:62';
        } else if(this.is_5_3()) {
            name='右左结构:53';

        } else if(this.is_3_7()) {
            name='对角结构:37';
        } else if(this.is_5_1()) {
            name='对角结构:51';
        } else if(this.is_1_5()) {
            name='对角结构:15';
        } else if(this.is_7_3()) {
            name='对角结构:73';
        }

        return name;
    }
    
    // x方向的位置
    public is_x_left(t) {
        return t==ObjPositionAlign.ALIGN_POINT_5
            ||t==ObjPositionAlign.ALIGN_POINT_6
            ||t==ObjPositionAlign.ALIGN_POINT_7
    }
    public is_x_middle(t) {
        return t==ObjPositionAlign.ALIGN_POINT_0
            ||t==ObjPositionAlign.ALIGN_POINT_4
            ||t==ObjPositionAlign.ALIGN_POINT_8
    }
    public is_x_right(t) {
        return t==ObjPositionAlign.ALIGN_POINT_1
            ||t==ObjPositionAlign.ALIGN_POINT_2
            ||t==ObjPositionAlign.ALIGN_POINT_3
    }
    public self_is_x_left(){return this.is_x_left(this.self);}
    public self_is_x_middle() { return this.is_x_middle(this.self); }
    public self_is_x_right() { return this.is_x_right(this.self); }
    public other_is_x_left() { return this.is_x_left(this.other); }
    public other_is_x_middle() { return this.is_x_middle(this.other); }
    public other_is_x_right() { return this.is_x_right(this.other); }
    
    // y方向的位置
    public is_y_top(t) {
        return t==ObjPositionAlign.ALIGN_POINT_7
            ||t==ObjPositionAlign.ALIGN_POINT_0
            ||t==ObjPositionAlign.ALIGN_POINT_1
    }
    public is_y_middle(t) {
        return t==ObjPositionAlign.ALIGN_POINT_6
            ||t==ObjPositionAlign.ALIGN_POINT_8
            ||t==ObjPositionAlign.ALIGN_POINT_2
    }
    public is_y_bottom(t) {
        return t==ObjPositionAlign.ALIGN_POINT_3
            ||t==ObjPositionAlign.ALIGN_POINT_4
            ||t==ObjPositionAlign.ALIGN_POINT_5
    }
    public self_is_y_top() { return this.is_y_top(this.self); }
    public self_is_y_middle() { return this.is_y_middle(this.self); }
    public self_is_y_bottom() { return this.is_y_bottom(this.self); }
    public other_is_y_top() { return this.is_y_top(this.other); }
    public other_is_y_middle() { return this.is_y_middle(this.other); }
    public other_is_y_bottom() { return this.is_y_bottom(this.other); }
    
    // 根据b跟a的位置关系计算出b相对于a的坐标
    // 注意，b只使用w/h参与计算
    // 参数：a 矩形a
    // 参数：b 矩形b
    // 参数：bPos 矩形b相对于a的类型
    // 参数：bOffset 偏移
    
    public static calPostionOfRelative(a: ObjPosition,b: ObjPosition,
        bPos: ObjRelativePosType,bOffset: Point):ObjPosition{
        var ret: ObjPosition=new ObjPosition();
        ret.copy(b);
            
        // 计算X－5种情况
        // b的右侧与a的左侧重合
        if(bPos.self_is_x_right() && bPos.other_is_x_left()){
            ret.x=a.x-b.w;
        }
        // b的左侧与a的左侧重合
        else if(bPos.self_is_x_left()&&bPos.other_is_x_left()) {
            ret.x=a.x
        }
        // b的中线与a的中线重合
        else if(bPos.self_is_x_middle() && bPos.other_is_x_middle()){
            ret.x=a.x+a.w/2-b.w/2;
        }
        // b的右侧侧与a的右侧重合
        else if(bPos.self_is_x_right()&&bPos.other_is_x_right()) {
            ret.x=a.x+a.w-b.w;
        }
        // b的左侧与a的右侧重合
        else{
            ret.x=a.x+a.w;
        }
        
        
        // 计算Y－5种情况
        // b的下侧与a的上侧重合
        if(bPos.self_is_y_bottom()&&bPos.other_is_y_top()) {
            ret.y=a.y-b.h;
        }
        // b的上侧与a的上侧重合
        else if(bPos.self_is_y_top()&&bPos.other_is_y_top()) {
            ret.y=a.y
        }
        // b的中线与a的中线重合
        else if(bPos.self_is_y_middle()&&bPos.other_is_y_middle()) {
            ret.y=a.y+a.h/2-b.h/2;
        }
        // b的下侧侧与a的下侧重合
        else if(bPos.self_is_y_bottom()&&bPos.other_is_y_bottom()) {
            ret.y=a.y+a.h-b.h;
        }
        // b的上侧与a的下侧重合
        else {
            ret.y=a.y+a.h;
        }
        
        // 添加偏移
        ret.x+=bOffset.x;
        ret.y+=bOffset.y;
        
        return ret;
            
    }
    
    // 计算两个pos的相对位置,以a为中心
    // 返回：Object类型，包括以下属性：
    //          type:ObjRelativePosType
    //          offset:Point
    public static calcRelativePosType(a: ObjPosition,b: ObjPosition): Object {
        /*
                |       |
           o7   |   o0  |   o1
         ----------------------
                |       |
           o6   |   a   |   o2
                |       |
         ----------------------
           o5   |  o4   |   o3
                |       |         
         */
        
         // 计算b的中心点与a的相对位置
        var o: Point=new Point(b.x+b.w/2,b.y+b.h/2);
        var aType: ObjRelativePosType=new ObjRelativePosType(ObjPositionAlign.ALIGN_POINT_1,ObjPositionAlign.ALIGN_POINT_7);
        
        if(o.x>a.x && o.x<a.x+a.w && o.y<a.y){
            // o0区域按照中间对齐
            aType.self=ObjPositionAlign.ALIGN_POINT_0;
            aType.other=ObjPositionAlign.ALIGN_POINT_4;
            
        }else if(o.x>a.x+a.w && o.y<a.y){
            // o1区域
            aType.self=ObjPositionAlign.ALIGN_POINT_1;
            aType.other=ObjPositionAlign.ALIGN_POINT_5;
        } else if(o.x>a.x+a.w&&o.y>a.y&&o.y<a.y+a.h) {
            // o2区域按照顶部对齐
            aType.self=ObjPositionAlign.ALIGN_POINT_1;
            aType.other=ObjPositionAlign.ALIGN_POINT_7;
        } else if(o.x>a.x+a.w&&o.y>a.y+a.h) {
            // o3区域
            aType.self=ObjPositionAlign.ALIGN_POINT_3;
            aType.other=ObjPositionAlign.ALIGN_POINT_7;
        } else if(o.x>a.x&&o.x<a.x+a.w&&o.y>a.y+a.h) {
            // o4区域按照中间对齐
            aType.self=ObjPositionAlign.ALIGN_POINT_4;
            aType.other=ObjPositionAlign.ALIGN_POINT_0;
        } else if(o.x<a.x&&o.y>a.y+a.h) {
            // o5区域
            aType.self=ObjPositionAlign.ALIGN_POINT_5;
            aType.other=ObjPositionAlign.ALIGN_POINT_1;
        } else if(o.x>a.x+a.w&&o.y>a.y&&o.y<a.y+a.h) {
            // o6区域按照顶部对齐
            aType.self=ObjPositionAlign.ALIGN_POINT_7;
            aType.other=ObjPositionAlign.ALIGN_POINT_1;
        } else if(o.x<a.x&&o.y<a.y) {
            // o5区域
            aType.self=ObjPositionAlign.ALIGN_POINT_7;
            aType.other=ObjPositionAlign.ALIGN_POINT_3;
        }
        
//        // 计算偏移
//        var newPos: ObjPosition=ObjRelativePosType.calPostionOfRelative(b,a,aType,new Point(0,0)); // 根据type计算偏移T1
//        var a_r_b: Point=new Point(a.x-b.x,a.y-b.y);// a相对于b的偏移T2
//        var aOffset: Point=new Point(a_r_b.x-newPos.x,a_r_b.y-newPos.y);//T1、T2的偏差 
        var aOffset: Point=new Point(0,0);
        //  return
        return { type: aType,offset: aOffset};
    }
    
}
